<?php
abstract class ui
{
	function __construct()
	{
		$this->init(__FILE__,'span');
	}
	
	function init($file,$tag,$attributes=array(),$allow_quickclose=false)
	{
		$this->attributes = array();
		$this->class = get_class($this);
		$this->nodes = array();
		$this->path  = dirname($file);
		$this->tag   = $tag;
		$this->html  = '';
		$this->js    = '';
		$this->last  = null;
		$this->data  = null;
		$this->parent= null;
		$this->render= true;
		$this->pre_inner_html  = '';
		$this->pre_outer_html  = '';
		$this->post_inner_html = '';
		$this->post_outer_html = '';
		foreach($attributes as $name=>$value)
		{
			$this->set($name,$value);
		}
		$this->allow_quickclose = $allow_quickclose;
	}
	
	function pre_render(){}
	function post_render(){}
	function pre_node_render(){}
	function post_node_render(){}
	
	function render($data=null)
	{
		global $ti;

		if(!is_null($data))
		{
			
			$this->data = $data;
		}

		#$ti->log('data is: '.$this->class.'/'.print_r($this->data,true));
		$this->pre_render($this->data);
		
		$this->html .= $this->pre_outer_html;
		
		if($this->render)
		{
			$this->html .= '<'.$this->tag;
			foreach($this->attributes as $name=>$value)
			{
				$this->html .= ' '.$name.'="'.$this->get($name).'"';
			}

			if($this->allow_quickclose && count($this->nodes) == 0)
			{
				$this->html .= ' />'.$this->post_outer_html;
			}
			else
			{
				$this->html .= '>'.$this->pre_inner_html;
				$this->pre_node_render($this->data);
				$this->render_nodes($this->data);
				$this->post_node_render($this->data);
				$this->html .= $this->post_inner_html.'</'.$this->tag.'>';
				
				$this->html .= $this->post_outer_html;
			}
		}
		else
		{
			$this->html .= $this->post_outer_html;
		}
		$this->post_render($this->data);
		return $this->html;
	}
	
	function render_nodes()
	{
		foreach($this->nodes as $index=>$node)
		{
			
			if(is_string($node))
			{
				$this->html .= $node;
			}
			else
			{
				$node->render($this->data);
				$this->html .= $node->html;
				$this->js   .= $node->js;
			}
		}
	}
	
	function text($text)
	{
		$this->nodes[] = $text;
	}
	
	function add()
	{
		$args = func_get_args();
		foreach($args as $arg)
		{
			$arg->parent =& $this;
			$this->nodes[] = $arg;
			$this->last  = $arg;
		}
		return $this;
	}
	
	function set($name,$value,$data=true)
	{
		$method = 'set_'.$name;
		if(method_exists($this,$method))
		{
			return $this->$method($value,$data);
		}
		else
		{
			$this->attributes[$name] = $value;
			return $this;
		}
	}
	
	function get($name)
	{
		$method = 'get_'.$name;
		if(method_exists($this,$method))
		{
			return $this->$method();
		}
		else
		{
			return $this->attributes[$name];
		}
	}
	
	function set_class($new_class,$state=true)
	{
		#echo('set class called: '.$new_class.'/'.$state);
		if(empty($this->attributes['class']))
		{
			$this->attributes['class'] = array();
		}
		$this->attributes['class'][$new_class] = $state;
	}
	
	function set_style($new_style,$value)
	{
		if(empty($this->attributes['style']))
		{
			$this->attributes['style'] = array();
		}
		$this->attributes['style'][$new_style] = $value;
	}
		
	function get_class()
	{
		#echo('get class called');
		$to_return = '';
		if(is_array($this->attributes['class']))
		{
			foreach($this->attributes['class'] as $class=>$state)
			{
				if($state)
				{
					$to_return .= (($to_return == '')?'':' ').$class;
				}
			}
		}
		return $to_return;
	}
		
	function get_style()
	{
		$to_return = '';
		if(is_array($this->attributes['style']))
		{
			foreach($this->attributes['style'] as $style=>$value)
			{
				$to_return .= $style.':'.$value.';';
			}
		}
		return $to_return;
	}
	
	function position($name)
	{
		global $ti;
		if(empty($ti->swap[$name]))
		{
			$ti->swap[$name] = '';
		}
		$this->add('{'.$name.'}');
		$this->set('id',$name);
		return $this;
	}
	
	function pad($amount)
	{
		$this->pre_inner_html .= '<div style="margin: '.$amount.'px;">';
		$this->post_inner_html = '</div>'.$this->post_inner_html;
	}
	
	function br()
	{
		$this->text('<br />');
		return $this;
	}
	
	function clear()
	{
		$this->text('<div style="clear:both;"></div>');
		return $this;
	}
	
	function generate_id()
	{
		if(empty($this->attributes['id']))
		{
			$this->set('id',md5(rand(0,100000)));
		}
		return $this;
	}
	
	function set_full_area()
	{
		$this->set('class','full_area');
		return $this;
	}
}
?>